package com.eboss.sbs.activity;

import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.PointActivateShopListAdapter;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.PointActivateShop;
import com.eboss.sbs.model.PointActivateShopResult;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.views.MyExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PointActvateShopListAcvitiy extends BaseActivity {

	private PullToRefreshScrollView pullToRefreshScrollView;
	
	private MyExpandableListView listview;
	private BaseExpandableListAdapter adapter;
	private int activateId;
	private List<PointActivateShop> datas = new ArrayList<PointActivateShop>();
	
	private int page = 1;
	private int rows = Config.ROWS;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_point_activate_shoplist);
		super.onCreate(savedInstanceState);
		initView();
		getData();
	}
	
	private void initView(){
		pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pullToRefreshScrollView);
		pullToRefreshScrollView.setOnRefreshListener(new MyOnRefreshListener());
		pullToRefreshScrollView.setMode(Mode.BOTH);
		
		activateId = getIntent().getIntExtra("id", -1);
		setTvText(R.id.tv_title, "活动店铺");
		listview = (MyExpandableListView)findViewById(R.id.listview);
		adapter = new PointActivateShopListAdapter(this, datas);
		listview.setAdapter(adapter);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
	}
	
	class MyOnRefreshListener implements OnRefreshListener2<ScrollView> {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			pullToRefreshScrollView.setMode(Mode.BOTH);
			String label = DateUtils.formatDateTime(mActivity, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
			refreshData();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			page++;
			getData();
		}
	}
	
	private void refreshData(){
		datas.clear();
		page = 1;
		getData();
	}
	
	private void getData(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("id", activateId);
		params.put("pagenum", page);
		params.put("pagesize", rows);
		API.http_post(this, API.POINT_ACTIVATE_SHOP_DETAIL, params, new HttpCallback<PointActivateShopResult>(this) {
			@Override
			public void onSuccess(int statusCode, PointActivateShopResult result) {
				pullToRefreshScrollView.onRefreshComplete();
				List<PointActivateShop> tmp_datas = result.getShops();
				
				if(tmp_datas==null||tmp_datas.size()==0||tmp_datas.size()<rows){
					if(tmp_datas==null||tmp_datas.size()==0){
						toast("没有更多数据");
					}
					pullToRefreshScrollView.setMode(Mode.PULL_DOWN_TO_REFRESH);
				}
				if(tmp_datas!=null&&tmp_datas.size()>0){
					datas.addAll(tmp_datas);
				}
				adapter.notifyDataSetInvalidated();
				
		        for (int i=0; i<adapter.getGroupCount(); i++) {
		        	listview.expandGroup(i);
		        };
				
			}
			
			@Override
			public void onFailure(int statusCode, String message) {
				pullToRefreshScrollView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<PointActivateShopResult>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.titleback_btn_back){finish();}
		super.onClick(v);
	}
	
}
